import type { Attendance } from '@/types/attendence';
import request from '@/utils/request';
import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useAttendanceStore = defineStore('attendance', () => {
    const attendances = ref<{ attendanceId: number; studentId: number; courseId: string; attendanceStatus: string }[]>([]);

    const addAttendance = (attendance: Attendance) => {
        request.post(`/api/attendances`, attendance);
    };

    const updateAttendance = (index: number, updatedAttendance: Attendance) => {
        
    };

    const deleteAttendance = (index: number) => {
        
    };

    const recordAttendance = (studentNumber: string,courseScheduleId:number ,status: string) => {
        const newStatus = status === 'absent'? '缺勤' :
        status === 'present'? '出勤' :
        status === 'late'? '迟到' :
        status === 'leaveEarly'? '早退' :
        status;

        const newAttendance :Attendance = {
            attendanceDate: Date.now(),
            studentNumber,
            courseScheduleId, 
            attendanceStatus: newStatus
        };

        addAttendance(newAttendance);
    };

    return {
        attendances,
        addAttendance,
        updateAttendance,
        deleteAttendance,
        recordAttendance
    };
});